<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class attend_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();
		$this->CI->load->model('Attend_model');
		$this->CI->load->model('Attend_student_status_model');
		$this->CI->load->model('Attend_students_norm_model');
		$this->CI->load->model('Attend_teaching_content_model');
		$this->CI->load->model('Attends_teaching_content_model');
		$this->CI->load->model('Attend3_teaching_content_model');
		$this->CI->load->model('Attend4_teaching_content_model');
		$this->CI->load->model('Attend_teaching_effect_model');
		$this->CI->load->model('Attend_teaching_implementation_model');
		$this->CI->load->model('Attend_teaching_level_model');
		$this->CI->load->model('Attend_teaching_readiness_model');
		$this->CI->load->model('Attend_classroom_facilities_model');
        $this->CI->load->model('Attend_classroom_environmental_model');
		$this->CI->load->model('Attend_record_model');
		$this->CI->load->model('Attend_img_model');
		$this->CI->load->model('Class_model');
		$this->CI->load->model('Admin_model');
	}

	/**  
	* 获取听课表基础数据（仅主表）
	* @access public 
	* @param  $attend_id 听课表id
	* @return array or bool
	*/  
	public function getAttendById($attend_id) {
		$attend=$this->CI->Attend_model->get_single(array('id'=>$attend_id));
		if($attend&&count($attend)>0){
			$attend['listen_time'] =  $attend['listen_time']==0?'':$attend['listen_time'];
			return $attend;
		}
		else{
			return false;
		}
	}
	
	/**  
	* 获取听课表页面数据
	* @access public 
	* @param  $attend_id 听课表id 当为新增时传递0
	* @param  $class_id 课程id 新增时传递 default false
	* @param  $listen_id 听课人id 新增时传递 default false
	* @return array
	*/  
	public function getAttendData($attend_id,$class_id=false,$listen_id=false,$type=false) {


	
		//主表数据
		$attend=$this->getAttendById($attend_id);
		//两种情况，1：有数据  2：没数据
		if($attend){
			// 理论课教学内容得分表（attend_teaching_content）
			$attend_teaching_content=$this->CI->Attend_teaching_content_model->get_list(array('attend_id'=>$attend_id));
			if(!empty($attend_teaching_content)){
				foreach ((new TeachingContent)->getAll() as $k => $v) {
					$attend_teaching_content[$k]['score']=$v[XPHEnum::Remark];
				}
			}else{
				foreach ((new TeachingContent)->getAll() as $k => $v) {
					$attend_teaching_content[$k]=array(
						'list_id'=>$v[XPHEnum::Alternate],
						'key_index'=>$v[XPHEnum::Value],
						'content'=>$v[XPHEnum::Caption],
						'score'=>$v[XPHEnum::Remark],
						'score_level'=>$v[XPHEnum::Detail],
						'get_score'=>''
						);
				}
			}

			//实训课的得分表
			$attends_teaching_content=$this->CI->Attends_teaching_content_model->get_list(array('attend_id'=>$attend_id));
			if(!empty($attends_teaching_content)){
				foreach ((new AttendsTeachingContent)->getAll() as $k => $v) {
					$attends_teaching_content[$k]['score']=$v[XPHEnum::Remark];
				}
			}else{
				foreach ((new AttendsTeachingContent)->getAll() as $k => $v) {
					$attends_teaching_content[$k]=array(
						'list_id'=>$v[XPHEnum::Alternate],
						'key_index'=>$v[XPHEnum::Value],
						'content'=>$v[XPHEnum::Caption],
						'score'=>$v[XPHEnum::Remark],
						'score_level'=>$v[XPHEnum::Detail],
						'get_score'=>''
						);
				}
			}

			//思政课的得分表
			$attend3_teaching_content=$this->CI->Attend3_teaching_content_model->get_list(array('attend_id'=>$attend_id));
			if(!empty($attend3_teaching_content)){
				foreach ((new Attend3TeachingContent)->getAll() as $k => $v) {
					$attend3_teaching_content[$k]['score']=$v[XPHEnum::Remark];
				}
			}else{
				foreach ((new Attend3TeachingContent)->getAll() as $k => $v) {
					$attend3_teaching_content[$k]=array(
						'list_id'=>$v[XPHEnum::Alternate],
						'key_index'=>$v[XPHEnum::Value],
						'content'=>$v[XPHEnum::Caption],
						'score'=>$v[XPHEnum::Remark],
						'score_level'=>$v[XPHEnum::Detail],
						'get_score'=>''
						);
				}
			}

			//体育课的得分表
			$attend4_teaching_content=$this->CI->Attend4_teaching_content_model->get_list(array('attend_id'=>$attend_id));
			if(!empty($attend4_teaching_content)){
				foreach ((new Attend4TeachingContent)->getAll() as $k => $v) {
					$attend4_teaching_content[$k]['score']=$v[XPHEnum::Remark];
				}
			}else{
				foreach ((new Attend4TeachingContent)->getAll() as $k => $v) {
					$attend4_teaching_content[$k]=array(
						'list_id'=>$v[XPHEnum::Alternate],
						'key_index'=>$v[XPHEnum::Value],
						'content'=>$v[XPHEnum::Caption],
						'score'=>$v[XPHEnum::Remark],
						'score_level'=>$v[XPHEnum::Detail],
						'get_score'=>''
						);
				}
			}
	

			// 教学实施得分表（attend_teaching_implementation）
			$attend_teaching_implementation=$this->CI->Attend_teaching_implementation_model->get_list(array('attend_id'=>$attend_id));
			if(!empty($attend_teaching_implementation)){
				foreach ((new TeachingImplementation)->getAll() as $k => $v) {
					$attend_teaching_implementation[$k]['score']=$v[XPHEnum::Remark];
				}
			}else{
				$attend_teaching_implementation=array();
				foreach ((new TeachingImplementation)->getAll()  as $k => $v) {
					$attend_teaching_implementation[$k]=array(					
						'list_id'=>$v[XPHEnum::Alternate],
						'key_index'=>$v[XPHEnum::Value],
						'content'=>$v[XPHEnum::Caption],
						'score'=>$v[XPHEnum::Remark],
						'get_score'=>''
						);
				}	
			}

			
			// 教学水平总体评价表（attend_teaching_level）
			$attend_teaching_level=$this->CI->Attend_teaching_level_model->get_list(array('attend_id'=>$attend_id));

			// 学生状况得分表（attend_student_status）
			$attend_student_status=$this->CI->Attend_student_status_model->get_list(array('attend_id'=>$attend_id));
	
			if(!empty($attend_student_status)){
				foreach($attend_student_status as $k=>$v){
					if($type == 1){
						$attend_student_status[$k]['score']=StudentStatus::First[XPHEnum::Remark];
					}else if($type == 2){
						$attend_student_status[$k]['score']=StudentStatus::Second[XPHEnum::Remark];
					}else if($type == 3){
						$attend_student_status[$k]['score']=StudentStatus::Third[XPHEnum::Remark];
					}else if($type == 4){
						$attend_student_status[$k]['score']=StudentStatus::Fourth[XPHEnum::Remark];
					}
				}

			}else{
				if($type == 1){//理论
					$attend_student_status[0]=array(					
						'list_id'=>StudentStatus::First[XPHEnum::Alternate],
						'key_index'=>StudentStatus::First[XPHEnum::Value],
						'content'=>StudentStatus::First[XPHEnum::Caption],
						'score'=>StudentStatus::First[XPHEnum::Remark],
						'get_score'=>''
						);
				}else if($type == 2){//实训
					$attend_student_status[0]=array(					
						'list_id'=>StudentStatus::Second[XPHEnum::Alternate],
						'key_index'=>StudentStatus::Second[XPHEnum::Value],
						'content'=>StudentStatus::Second[XPHEnum::Caption],
						'score'=>StudentStatus::Second[XPHEnum::Remark],
						'get_score'=>''
						);
				}else if($type == 3){//思政课
					$attend_student_status[0]=array(					
						'list_id'=>StudentStatus::Third[XPHEnum::Alternate],
						'key_index'=>StudentStatus::Third[XPHEnum::Value],
						'content'=>StudentStatus::Third[XPHEnum::Caption],
						'score'=>StudentStatus::Third[XPHEnum::Remark],
						'get_score'=>''
						);
				}else if($type == 4){//体育课
					$attend_student_status[0]=array(					
						'list_id'=>StudentStatus::Fourth[XPHEnum::Alternate],
						'key_index'=>StudentStatus::Fourth[XPHEnum::Value],
						'content'=>StudentStatus::Fourth[XPHEnum::Caption],
						'score'=>StudentStatus::Fourth[XPHEnum::Remark],
						'get_score'=>''
						);
				}			
			}
			
			// pri($attend_student_status);
			
			// 教学效果总体评价表（attend_teaching_effect）
			$attend_teaching_effect=$this->CI->Attend_teaching_effect_model->get_list(array('attend_id'=>$attend_id));
			// 教学准备开展情况表（attend_teaching_readiness）
			$attend_teaching_readiness=$this->CI->Attend_teaching_readiness_model->get_list(array('attend_id'=>$attend_id));
			// 学生常态表（attend_students_norm）
			$attend_students_norm=$this->CI->Attend_students_norm_model->get_list(array('attend_id'=>$attend_id));
			// 课堂设施设备完好情况（attend_classroom_facilities）
			$attend_classroom_facilities=$this->CI->Attend_classroom_facilities_model->get_single(array('attend_id'=>$attend_id));
			// 课堂环境卫生情况表（attend_classroom_environmental）
			$attend_classroom_environmental=$this->CI->Attend_classroom_environmental_model->get_single(array('attend_id'=>$attend_id));
			// 听课内容记录表（attend_record）
			$attend_record=$this->CI->Attend_record_model->get_list(array('attend_id'=>$attend_id));
			// 听课图片表（attend_img）听课记录
			$attend_img1=$this->CI->Attend_img_model->get_list(array('attend_id'=>$attend_id,'type'=>AttendImgType::First[XPHEnum::Value]));
			$attend_img2=$this->CI->Attend_img_model->get_list(array('attend_id'=>$attend_id,'type'=>AttendImgType::Second[XPHEnum::Value]));
			$attend_img3=$this->CI->Attend_img_model->get_list(array('attend_id'=>$attend_id,'type'=>AttendImgType::Third[XPHEnum::Value]));
		}
		else{
			
			//主表数据
			$connar['cond']=array('class.id'=>$class_id);
			$connar['join']=array('admin a','a.id=class.teacher_id');
			$connar['items']=" class.*,a.username,a.title,a.dept_name,a.age_groups,a.teachers_attribute ";

			
			$class=$this->CI->Class_model->get_single_full($connar);
			$listen=$this->CI->Admin_model->get_single(array('id'=>$listen_id));
			$attend=array(
				'class_id'=>$class_id,
				'listen_id'=>$listen_id,
				'serial_no'=>buildOrdeNo(),
				'academic_year'=>'',
				'semester'=>'',
				'class_name'=>isset($class['name'])?$class['name']:'',
				'class_type'=>isset($class['type'])?$class['type']:'',
				'cc_type'=>isset($class['cc_type'])?$class['cc_type']:'',
				'teacher_name'=>isset($class['username'])?$class['username']:'',
				'teacher_code'=>'',
				'title'=>isset($class['title'])?$class['title']:'',
				'dept_name'=>isset($class['dept_name'])?$class['dept_name']:'',
				'age_groups'=>isset($class['age_groups'])?$class['age_groups']:'',
				'teachers_attribute'=>isset($class['teachers_attribute'])?$class['teachers_attribute']:'',
				'listen_name'=>$listen['username'],
				'listen_time'=>time(),
				'listen_address'=>'',
				'course_units'=>isset($class['course_units'])?$class['course_units']:'',
				'listen_class'=>isset($class['classroom'])?$class['classroom']:'',
				// 'classroom_facilities'=>'',
				// 'classroom_environmental'=>'',
				'advice'=>'',
				'teaching_level_score'=>'',
				'teaching_effect_score'=>'',
				'created'=>time()
				);
			// 教学内容得分表（attend_teaching_content）
			$_attend_teaching_content=(new TeachingContent)->getAll();

			
			$attend_teaching_content=array();
			foreach ($_attend_teaching_content as $k => $v) {
				$attend_teaching_content[$k]=array(
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'score_level'=>$v[XPHEnum::Detail],
					'get_score'=>''
					);
			}

			// 实训课教学能力得分表（attend_teaching_content）
			$_attends_teaching_content=(new AttendsTeachingContent)->getAll();
			$attends_teaching_content=array();
			foreach ($_attends_teaching_content as $k => $v) {
				$attends_teaching_content[$k]=array(
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'score_level'=>$v[XPHEnum::Detail],
					'get_score'=>''
					);
			}

			//思政课教学能力得分表（attend_teaching_content）
			$_attend3_teaching_content=(new Attend3TeachingContent)->getAll();
			$attend3_teaching_content=array();
			foreach ($_attend3_teaching_content as $k => $v) {
				$attend3_teaching_content[$k]=array(
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'score_level'=>$v[XPHEnum::Detail],
					'get_score'=>''
					);
			}

			
			//体育课教学能力得分表（attend_teaching_content）
			$_attend4_teaching_content=(new Attend4TeachingContent)->getAll();
			$attend4_teaching_content=array();
			foreach ($_attend4_teaching_content as $k => $v) {
				$attend4_teaching_content[$k]=array(
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'score_level'=>$v[XPHEnum::Detail],
					'get_score'=>''
					);
			}


			// 教学实施得分表（attend_teaching_implementation）
			$_attend_teaching_implementation=(new TeachingImplementation)->getAll();
			$attend_teaching_implementation=array();
			foreach ($_attend_teaching_implementation as $k => $v) {
				$attend_teaching_implementation[$k]=array(					
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'get_score'=>''
					);
			}
			// 教学水平总体评价表（attend_teaching_level）
			$_attend_teaching_level=(new TeachingLevel)->getAll();
			$attend_teaching_level=array();
			foreach ($_attend_teaching_level as $k => $v) {
				$attend_teaching_level[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'remark'=>$v[XPHEnum::Remark],
					'is_check'=>CommonWhether::Not[XPHEnum::Value]
					);
			}
			// 学生状况得分表（attend_student_status）
			if($type == 1){
				$attend_student_status[0]=array(					
					'list_id'=>StudentStatus::First[XPHEnum::Alternate],
					'key_index'=>StudentStatus::First[XPHEnum::Value],
					'content'=>StudentStatus::First[XPHEnum::Caption],
					'score'=>StudentStatus::First[XPHEnum::Remark],
					'get_score'=>''
					);
			}else if($type == 2){
				$attend_student_status[0]=array(					
					'list_id'=>StudentStatus::Second[XPHEnum::Alternate],
					'key_index'=>StudentStatus::Second[XPHEnum::Value],
					'content'=>StudentStatus::Second[XPHEnum::Caption],
					'score'=>StudentStatus::Second[XPHEnum::Remark],
					'get_score'=>''
					);
			}else if($type == 3){
				$attend_student_status[0]=array(					
					'list_id'=>StudentStatus::Third[XPHEnum::Alternate],
					'key_index'=>StudentStatus::Third[XPHEnum::Value],
					'content'=>StudentStatus::Third[XPHEnum::Caption],
					'score'=>StudentStatus::Third[XPHEnum::Remark],
					'get_score'=>''
					);
			}else if($type == 4){
				$attend_student_status[0]=array(					
					'list_id'=>StudentStatus::Fourth[XPHEnum::Alternate],
					'key_index'=>StudentStatus::Fourth[XPHEnum::Value],
					'content'=>StudentStatus::Fourth[XPHEnum::Caption],
					'score'=>StudentStatus::Fourth[XPHEnum::Remark],
					'get_score'=>''
					);
			}
			// $_attend_student_status=(new StudentStatus)->getAll();
			// $attend_student_status=array();
			// foreach ($_attend_student_status as $k => $v) {
			// 	$attend_student_status[$k]=array(
			// 		'list_id'=>$v[XPHEnum::Alternate],
			// 		'key_index'=>$v[XPHEnum::Value],
			// 		'content'=>$v[XPHEnum::Caption],
			// 		'score'=>$v[XPHEnum::Remark],
			// 		'get_score'=>''
			// 		);
			// }
			// 教学效果总体评价表（attend_teaching_effect）
			$_attend_teaching_effect=(new TeachingEffect)->getAll();
			$attend_teaching_effect=array();
			foreach ($_attend_teaching_effect as $k => $v) {
				$attend_teaching_effect[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'remark'=>$v[XPHEnum::Remark],
					'is_check'=>CommonWhether::Not[XPHEnum::Value]
					);
			}
			// 教学准备开展情况表（attend_teaching_readiness）
			$_attend_teaching_readiness=(new TeachingReadiness)->getAll();
			$attend_teaching_readiness=array();
			foreach ($_attend_teaching_readiness as $k => $v) {
				$attend_teaching_readiness[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'option'=>implode(',',$v[XPHEnum::Remark]),
					'check_value'=>''
					);
			}
			// 学生常态表（attend_students_norm）
			$_attend_students_norm=(new StudentsNorm)->getAll();
			$attend_students_norm=array();
			foreach ($_attend_students_norm as $k => $v) {
				$attend_students_norm[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'check_value'=>''
					);
			}
			// 课堂设施设备完好情况（attend_classroom_facilities）
			$_attend_classroom_facilities=(new ClassroomFacilities)->getAll();
			$attend_classroom_facilities_option=array_column($_attend_classroom_facilities, XPHEnum::Caption);
			$attend_classroom_facilities=array('option'=>implode(',',$attend_classroom_facilities_option),'check_value'=>'');
			// 课堂环境卫生情况表（attend_classroom_environmental）
			$_attend_classroom_environmental=(new ClassroomEnvironmental)->getAll();
			$attend_classroom_environmental_option=array_column($_attend_classroom_environmental, XPHEnum::Caption);
			$attend_classroom_environmental=array('option'=>implode(',',$attend_classroom_environmental_option),'check_value'=>'');
			// 听课内容记录表（attend_record）
			$attend_record=array();
			// 听课图片表（attend_img）
			$attend_img1=array();
			$attend_img2=array();
			$attend_img3=array();
		}
		// var_dump($attend_student_status);
		
		$result=array(
			'attend'=>$attend,
			'attend_teaching_content'=>$attend_teaching_content,
			'attends_teaching_content'=>$attends_teaching_content,//实训课得分表
			'attend3_teaching_content'=>$attend3_teaching_content,//思政课得分表
			'attend4_teaching_content'=>$attend4_teaching_content,//体育课得分表
			'attend_teaching_implementation'=>$attend_teaching_implementation,
			'attend_teaching_level'=>$attend_teaching_level,
			'attend_student_status'=>$attend_student_status,
			'attend_teaching_effect'=>$attend_teaching_effect,
			'attend_teaching_readiness'=>$attend_teaching_readiness,
			'attend_students_norm'=>$attend_students_norm,
			'attend_classroom_facilities'=>$attend_classroom_facilities,
			'attend_classroom_environmental'=>$attend_classroom_environmental,
			'attend_record'=>$attend_record,
			'attend_img1'=>$attend_img1,
			'attend_img2'=>$attend_img2,
			'attend_img3'=>$attend_img3
			);
		return $result;
	}


	/**  
	* 听课表数据保存
	* @access public 
	* @param  $param 表单数据
	* @param  $attend_id 听课表id 新增为0 default 0
	* @return int
	*/  
	public function saveAttend($param,$attend_id=0) {
		//主表数据
		$attend_default=$this->getAttendById($attend_id);

		//主表操作
		$attend_param=$param['attend'];
		$attend=array(
			'class_id'=>$attend_param['class_id'],
			'listen_id'=>$attend_param['listen_id'],
			'type'=>$attend_param['type'],
			// 'serial_no'=>$attend_param['serial_no'],
			'academic_year'=>$attend_param['academic_year'],
			'semester'=>$attend_param['semester'],
			'class_name'=>$attend_param['class_name'],
			'class_type'=>$attend_param['class_type'],
			'cc_type'=>$attend_param['cc_type'],
			'teacher_name'=>$attend_param['teacher_name'],
			'teacher_code'=>$attend_param['teacher_code'],
			'title'=>$attend_param['title'],
			'dept_name'=>$attend_param['dept_name'],
			'age_groups'=>$attend_param['age_groups'],
			'teachers_attribute'=>$attend_param['teachers_attribute'],
			'listen_name'=>$attend_param['listen_name'],
			'listen_time'=>strtotime($attend_param['listen_time']),
			'listen_address'=>$attend_param['listen_address'],
			'course_units'=>$attend_param['course_units'],
			'listen_class'=>$attend_param['listen_class'],
			// 'classroom_facilities'=>$attend_param['classroom_facilities'],
			// 'classroom_environmental'=>$attend_param['classroom_environmental'],
			'advice'=>$attend_param['advice'],
			'teaching_level_score'=>$attend_param['teaching_level_score'],
			'teaching_effect_score'=>$attend_param['teaching_effect_score'],
			'status'=>$attend_param['status'],
			// 0415新增总分 教学70%+学生30%
			'score'=>$attend_param['score'],
			// 0723新增听课类型
			'listen_type'=>$attend_param['listen_type']
		);
		//新增，添加created
		if(!$attend_default){
			$attend['created']=time();
			$attend_id=$this->CI->Attend_model->add($attend);
		}
		//修改，添加id和updated
		else{
			$attend['id']=$attend_id;
			$attend['updated']=time();
			$this->CI->Attend_model->update($attend);
		}

		//子表
		//数据清除
		$this->CI->Attend_teaching_content_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attends_teaching_content_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));//实训得分表
		$this->CI->Attend3_teaching_content_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));//思政课得分表
		$this->CI->Attend4_teaching_content_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));//体育课得分表
		$this->CI->Attend_teaching_implementation_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_teaching_level_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_student_status_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_teaching_effect_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_teaching_readiness_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_students_norm_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_classroom_facilities_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_classroom_environmental_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_record_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_img_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		//定值
		$attend_teaching_content=isset($param['attend_teaching_content'])?$param['attend_teaching_content']:'';
		$attends_teaching_content=isset($param['attends_teaching_content'])?$param['attends_teaching_content']:'';
		$attend3_teaching_content=isset($param['attend3_teaching_content'])?$param['attend3_teaching_content']:'';
		$attend4_teaching_content=isset($param['attend4_teaching_content'])?$param['attend4_teaching_content']:'';
		$attend_teaching_implementation=isset($param['attend_teaching_implementation'])?$param['attend_teaching_implementation']:'';
		$attend_teaching_level=isset($param['attend_teaching_level'])?$param['attend_teaching_level']:'';
		$attend_student_status=isset($param['attend_student_status'])?$param['attend_student_status']:'';
		$attend_teaching_effect=isset($param['attend_teaching_effect'])?$param['attend_teaching_effect']:'';
		/* $attend_teaching_readiness=$param['attend_teaching_readiness'];
		$attend_students_norm=$param['attend_students_norm'];
		$attend_classroom_facilities=$param['attend_classroom_facilities'];
		$attend_classroom_environmental=$param['attend_classroom_environmental']; */
		$attend_record=$param['attend_record'];
		$attend_img1=$param['attend_img1'];
		$attend_img2=$param['attend_img2'];
		$attend_img3=$param['attend_img3'];

		if(isset($attend_teaching_content)&&$attend_teaching_content!=""){
			// 教学内容得分表（attend_teaching_content）
			$default_attend_teaching_content_sql=$attend_teaching_content_sql=" insert into db_attend_teaching_content (attend_id,content,score,get_score,key_index,type,list_id) values ";
			foreach ($attend_teaching_content as $k => $v) {
				if($default_attend_teaching_content_sql!=$attend_teaching_content_sql){
					$attend_teaching_content_sql.=" , ";
				}
				$attend_teaching_content_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::Content[XPHEnum::Value],$v['list_id']));
			}
			$this->CI->Attend_teaching_content_model->query($attend_teaching_content_sql,false);
		}

		if(isset($attends_teaching_content)&&$attends_teaching_content!=""){
			// 实训课得分表（attends_teaching_content）
			$default_attends_teaching_content_sql=$attends_teaching_content_sql=" insert into db_attends_teaching_content (attend_id,content,score,get_score,key_index,type,list_id) values ";
			foreach ($attends_teaching_content as $k => $v) {
				if($default_attends_teaching_content_sql!=$attends_teaching_content_sql){
					$attends_teaching_content_sql.=" , ";
				}
				$attends_teaching_content_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::Content[XPHEnum::Value],$v['list_id']));
			}
			$this->CI->Attends_teaching_content_model->query($attends_teaching_content_sql,false);
		}

		if(isset($attend3_teaching_content)&&$attend3_teaching_content!=""){
			// 实训课得分表（attend3_teaching_content）
			$default_attend3_teaching_content_sql=$attend3_teaching_content_sql=" insert into db_attend3_teaching_content (attend_id,content,score,get_score,key_index,type,list_id) values ";
			foreach ($attend3_teaching_content as $k => $v) {
				if($default_attend3_teaching_content_sql!=$attend3_teaching_content_sql){
					$attend3_teaching_content_sql.=" , ";
				}
				$attend3_teaching_content_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::Content[XPHEnum::Value],$v['list_id']));
			}
			$this->CI->Attend3_teaching_content_model->query($attend3_teaching_content_sql,false);
		}

		if(isset($attend4_teaching_content)&&$attend4_teaching_content!=""){
			// 体育课得分表（attend4_teaching_content）
			$default_attend4_teaching_content_sql=$attend4_teaching_content_sql=" insert into db_attend4_teaching_content (attend_id,content,score,get_score,key_index,type,list_id) values ";
			foreach ($attend4_teaching_content as $k => $v) {
				if($default_attend4_teaching_content_sql!=$attend4_teaching_content_sql){
					$attend4_teaching_content_sql.=" , ";
				}
				$attend4_teaching_content_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::Content[XPHEnum::Value],$v['list_id']));
			}
			$this->CI->Attend4_teaching_content_model->query($attend4_teaching_content_sql,false);
		}

		if(isset($attend_teaching_implementation)&& $attend_teaching_implementation !=""){
		
			// 教学实施得分表（attend_teaching_implementation）
			$default_attend_teaching_implementation_sql=$attend_teaching_implementation_sql=" insert into db_attend_teaching_implementation (attend_id,content,score,get_score,key_index,type,list_id) values ";
			foreach ($attend_teaching_implementation as $k => $v) {
				if($default_attend_teaching_implementation_sql!=$attend_teaching_implementation_sql){
					$attend_teaching_implementation_sql.=" , ";
				}
				$attend_teaching_implementation_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::Implementation[XPHEnum::Value],$v['list_id']));
			}
			$this->CI->Attend_teaching_implementation_model->query($attend_teaching_implementation_sql,false);
		}

		if(isset($attend_teaching_level)&&$attend_teaching_level!=""){
			// 教学水平总体评价表（attend_teaching_level）
			$default_attend_teaching_level_sql=$attend_teaching_level_sql=" insert into db_attend_teaching_level (attend_id,item,remark,is_check) values ";
			foreach ($attend_teaching_level as $k => $v) {
				if($default_attend_teaching_level_sql!=$attend_teaching_level_sql){
					$attend_teaching_level_sql.=" , ";
				}
				$attend_teaching_level_sql.=$this->CI->sqlEscape(" (??,??,??,??) ",array($attend_id,$v['item'],$v['remark'],$v['is_check']));
			}
			$this->CI->Attend_teaching_level_model->query($attend_teaching_level_sql,false);
		}

		if(isset($attend_student_status)&&$attend_student_status!=""){
			// 学生状况得分表（attend_student_status）
			$default_attend_student_status_sql=$attend_student_status_sql=" insert into db_attend_student_status (attend_id,content,score,get_score,key_index,type,list_id) values ";
			foreach ($attend_student_status as $k => $v) {
				if($default_attend_student_status_sql!=$attend_student_status_sql){
					$attend_student_status_sql.=" , ";
				}
				$attend_student_status_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::StudentStatus[XPHEnum::Value],$v['list_id']));
			}
			$this->CI->Attend_student_status_model->query($attend_student_status_sql,false);
		}

		if(isset($attend_teaching_effect)&&$attend_teaching_effect!=""){
			// 教学效果总体评价表（attend_teaching_effect）
			$default_attend_teaching_effect_sql=$attend_teaching_effect_sql=" insert into db_attend_teaching_effect (attend_id,item,remark,is_check) values ";
			foreach ($attend_teaching_effect as $k => $v) {
				if($default_attend_teaching_effect_sql!=$attend_teaching_effect_sql){
					$attend_teaching_effect_sql.=" , ";
				}
				$attend_teaching_effect_sql.=$this->CI->sqlEscape(" (??,??,??,??) ",array($attend_id,$v['item'],$v['remark'],$v['is_check']));
			}
			$this->CI->Attend_teaching_effect_model->query($attend_teaching_effect_sql,false);
		}
		// 教学准备开展情况表（attend_teaching_readiness）
		/* $default_attend_teaching_readiness_sql=$attend_teaching_readiness_sql=" insert into db_attend_teaching_readiness (attend_id,item,`option`,check_value) values ";
		foreach ($attend_teaching_readiness as $k => $v) {
			if($default_attend_teaching_readiness_sql!=$attend_teaching_readiness_sql){
				$attend_teaching_readiness_sql.=" , ";
			}
			$attend_teaching_readiness_sql.=$this->CI->sqlEscape(" (??,??,??,??) ",array($attend_id,$v['item'],$v['option'],$v['check_value']));
		}
		$this->CI->Attend_teaching_readiness_model->query($attend_teaching_readiness_sql,false);

		// 学生常态表（attend_students_norm）
		$default_attend_students_norm_sql=$attend_students_norm_sql=" insert into db_attend_students_norm (attend_id,item,check_value) values ";
		foreach ($attend_students_norm as $k => $v) {
			if($default_attend_students_norm_sql!=$attend_students_norm_sql){
				$attend_students_norm_sql.=" , ";
			}
			$attend_students_norm_sql.=$this->CI->sqlEscape(" (??,??,??) ",array($attend_id,$v['item'],$v['check_value']));
		}
		$this->CI->Attend_students_norm_model->query($attend_students_norm_sql,false); */

		// 课堂设施设备完好情况表（attend_classroom_facilities）
	/* 	$this->CI->Attend_classroom_facilities_model->add(array('attend_id'=>$attend_id,'option'=>$attend_classroom_facilities['option'],'check_value'=>$attend_classroom_facilities['check_value']));

		// 课堂环境卫生情况表（attend_classroom_environmental）
		$this->CI->Attend_classroom_environmental_model->add(array('attend_id'=>$attend_id,'option'=>$attend_classroom_environmental['option'],'check_value'=>$attend_classroom_environmental['check_value'])); */

		// 听课内容记录表（attend_record）
		if(count($attend_record)>0){
			$default_attend_record_sql=$attend_record_sql=" insert into db_attend_record (attend_id,time,commentary) values ";
			foreach ($attend_record as $k => $v) {
				if($default_attend_record_sql!=$attend_record_sql){
					$attend_record_sql.=" , ";
				}
				$attend_record_sql.=$this->CI->sqlEscape(" (??,??,??) ",array($attend_id,$v['time'],$v['commentary']));
			}
			$this->CI->Attend_record_model->query($attend_record_sql,false);
		}

		// 听课图片表（attend_img）
		if(count($attend_img1)>0){
			$default_attend_img1_sql=$attend_img1_sql=" insert into db_attend_img (attend_id,img,type) values ";
			foreach ($attend_img1 as $k => $v) {
				if($default_attend_img1_sql!=$attend_img1_sql){
					$attend_img1_sql.=" , ";
				}
				$attend_img1_sql.=$this->CI->sqlEscape(" (??,??,??) ",array($attend_id,$v['img'],AttendImgType::First[XPHEnum::Value]));
			}
			$this->CI->Attend_img_model->query($attend_img1_sql,false);
		}

		// 意见或建议表（attend_img）
		if(count($attend_img2)>0){
			$default_attend_img2_sql=$attend_img2_sql=" insert into db_attend_img (attend_id,img,type) values ";
			foreach ($attend_img2 as $k => $v) {
				if($default_attend_img2_sql!=$attend_img2_sql){
					$attend_img2_sql.=" , ";
				}
				$attend_img2_sql.=$this->CI->sqlEscape(" (??,??,??) ",array($attend_id,$v['img'],AttendImgType::Second[XPHEnum::Value]));
			}
			$this->CI->Attend_img_model->query($attend_img2_sql,false);
		}

		//学生常态表（attend_img）
		if(count($attend_img3)>0){
			$default_attend_img3_sql=$attend_img3_sql=" insert into db_attend_img (attend_id,img,type) values ";
			foreach ($attend_img3 as $k => $v) {
				if($default_attend_img3_sql!=$attend_img3_sql){
					$attend_img3_sql.=" , ";
				}
				$attend_img3_sql.=$this->CI->sqlEscape(" (??,??,??) ",array($attend_id,$v['img'],AttendImgType::Third[XPHEnum::Value]));
			}
			$this->CI->Attend_img_model->query($attend_img3_sql,false);
		}

		return $attend_id;
	}

	// 听课数据分析2教师分类
	function getDatas($params,$isExport){
		$connar = array();
		if (!$isExport) {
			$param=$params;
			$connar = $this -> CI->Attend_model -> pickPages($connar, $params);
		}
		$where='where db_attend.status='.AttendStatus::Submit[XPHEnum::Value];
		$role=explode(',',getSess()['sub_role_id']);
		array_push($role,getSess()['role_id']);
		if(in_array(WebRole::Manage[XPHEnum::Value], $role)){
				$where.=' and 1=1';
			}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) && in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				$where.=' and dept_id='.getSess()['dept_id'].' and if(db_admin.id='.getSess()['id'].',1=1,(type='.AttendType::Unit[XPHEnum::Value].' or type='.AttendType::Outside[XPHEnum::Value].'))';
		}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) && !in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				$where.=' and dept_id='.getSess()['dept_id'].' and (db_attend.type='.AttendType::Unit[XPHEnum::Value].' and db_attend.type='.AttendType::Outside[XPHEnum::Value].')';		
			}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role)){
				$where.=' and db_admin.id='.getSess()['id'];		
			}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){

			}else if(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				$where.=' and db_admin.id='.getSess()['id'];
			}else if(in_array(WebRole::ExternalSupervisor[XPHEnum::Value], $role)){
				$where.=' and db_admin.id='.getSess()['id'];
			}else if(in_array(WebRole::SupervisorDirector[XPHEnum::Value], $role)){
				$where.=' and db_admin.id='.getSess()['id'];
			}else if(in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
				$where.=' and db_admin.id='.getSess()['id'];
			}

			if (isset($params['start_time']) && isset($params['end_time'])) {
				$where.=' and db_attend.listen_time>'.strtotime($params['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
			}else if (isset($param['start_time']) && isset($param['end_time'])) {
				$where.=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
			}

			if (isset($params['dept_name'])) {
				$where.=' and db_admin.dept_name like "%'.$params['dept_name'].'%"';
			}else if (isset($param['dept_name'])) {
				$where.=' and db_attend.dept_name like "%'.$param['dept_name'].'%"';
			}
			if (isset($params['username'])) {
				$where.=' and db_admin.username like "%'.$params['username'].'%"';
			}else if (isset($param['username'])) {
				$where.=' and db_admin.username like "%'.$param['username'].'%"';
			}
			if (isset($params['level']) && $params['level']!=-1) {
				if($params['level']==FeedBackSubType::School[XPHEnum::Value]){
					$level=WebRole::SchoolSupervisor[XPHEnum::Value];
				}
				if($params['level']==FeedBackSubType::Unit[XPHEnum::Value]){
					$level=WebRole::UnitSupervisor[XPHEnum::Value];
				}
				$where.=' and (role_id='.$level.' or find_in_set('.$level.',sub_role_id))';
			}else if (isset($param['level']) && $param['level']!=-1) {
				if($param['level']==FeedBackSubType::School[XPHEnum::Value]){
					$level=WebRole::SchoolSupervisor[XPHEnum::Value];
				}
				if($param['level']==FeedBackSubType::Unit[XPHEnum::Value]){
					$level=WebRole::UnitSupervisor[XPHEnum::Value];
				}
				$where.=' and (role_id='.$level.' or find_in_set('.$level.',sub_role_id))';
			}

			if(isset($params['content'])){
				$where.=' and db_attend_record.commentary like "%'.$param['content'].'%"';
			}else if (isset($param['content'])) {
				$where.=' and db_attend_record.commentary like "%'.$param['content'].'%"';
			}
			if(!$isExport){
				if(isset($params['pageNo']) && isset($params['pageSize'])){			
					$pageNo=$param['pageNo']-1;
					$pageSize=$param['pageSize'];
				}else{
					$pageNo='0';
					$pageSize='20';
				}
			}		
			if (!$isExport) {
				$data = $this -> CI->Attend_model -> get_page_list($connar);
				$data['ret']=$this -> CI->Attend_model->query('select * from(select listen_id,usercode,username from db_attend left join db_admin on db_admin.id=db_attend.listen_id left join db_attend_record on db_attend.id=db_attend_record.attend_id '.$where.') attend group by listen_id'.' limit '.$pageSize*$pageNo.','.$pageSize);
				$count=$this -> CI->Attend_model -> query('select count(*) count from(select count(*) count from(select listen_id,usercode,username from db_attend left join db_admin on db_admin.id=db_attend.listen_id left join db_attend_record on db_attend.id=db_attend_record.attend_id '.$where.') attend group by listen_id)a ');
				$data['count']=$count[0]['count'];
				$data['totalPage']=ceil($data['count']/$pageSize);
			} else {
				$data['ret']=$this -> CI->Attend_model->query('select * from(select listen_id,usercode,username from db_attend left join db_admin on db_admin.id=db_attend.listen_id left join db_attend_record on db_attend.id=db_attend_record.attend_id '.$where.') attend group by listen_id');
				$count=$this -> CI->Attend_model -> query('select count(*) count from(select count(*) count from(select listen_id,usercode,username from db_attend left join db_admin on db_admin.id=db_attend.listen_id left join db_attend_record on db_attend.id=db_attend_record.attend_id '.$where.') attend group by listen_id)a');
			}

			foreach ($data['ret'] as $k => $v) {
				// 以督导身份听课人数
				$connarnum['items']='count(DISTINCT teacher_name) attend_num';
				$connarnum['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Unit[XPHEnum::Value]));
				$connarnum['join'][0]=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				$connarnum['join'][1]=array('db_admin','db_admin.id=db_attend.listen_id');
				if(isset($params['content'])){
					$connarnum['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connarnum['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connarnum['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				if (isset($params['dept_name'])  && $params['dept_name']!='') {
					$connarnum['where'].=$this->CI->sqlLikeEscape(' and db_admin.dept_name like "%??%"',array($params['dept_name']));
				}else if (isset($param['dept_name']) && $param['dept_name']!='') {
					$connarnum['where'].=$this->CI->sqlLikeEscape(' and db_attend.dept_name like "%??%"',array($param['dept_name']));
				}
				if (isset($params['username'])   && $params['username']!='') {
					$connarnum['where'].=$this->CI->sqlLikeEscape(' and db_admin.username like "%??%"',array($params['username']));
				}else if (isset($param['username'])   && $param['username']!='') {
					$connarnum['where'].=$this->CI->sqlLikeEscape(' and db_admin.username like "%??%"',array($param['username']));
				}

				// if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) && in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				// 	$connar['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::Unit[XPHEnum::Value]));
				// }
				
				// $connar['groupby']='teacher_name';
				
				$num=$this->CI->Attend_model->get_single_full($connarnum);
				$data['ret'][$k]['attend_num']=$num['attend_num'];
				// 以督导身份听课次数
				$connar1['items']='COUNT(*) attend_count';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Unit[XPHEnum::Value]));
				$connar1['join'][0]=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				$connar1['join'][1]=array('db_admin','db_admin.id=db_attend.listen_id');
				if(isset($params['content'])){
					$connar1['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				if (isset($params['dept_name']) && $params['dept_name']!='') {
					$connar1['where'].=$this->CI->sqlEscape(' and db_admin.dept_name like "%??%"',array($params['dept_name']));
				}else if (isset($param['dept_name']) && $param['dept_name']!='') {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.dept_name like "%??%"',array($param['dept_name']));
				}
				if (isset($params['username'])&& $params['username']!='') {
					$connar1['where'].=$this->CI->sqlLikeEscape(' and db_admin.username like "%??%"',array($params['username']));
				}else if (isset($param['username'])&& $param['username']!='') {
					$connar1['where'].=$this->CI->sqlLikeEscape(' and db_admin.username like "%??%"',array($param['username']));
				}
				// if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) && in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				// 	$connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::Unit[XPHEnum::Value]));
				// }
				$count=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['attend_count']=$count['attend_count'];

				// 以校督导身份听课人数
				$connarnum['items']='count(DISTINCT teacher_name) attend_num';
				$connarnum['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::School[XPHEnum::Value]));
				$connarnum['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connarnum['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connarnum['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connarnum['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				
				$num=$this->CI->Attend_model->get_single_full($connarnum);
				$data['ret'][$k]['attend_num_school']=$num['attend_num'];
				// 以校督导身份听课次数
				$connar1['items']='COUNT(*) attend_count';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::School[XPHEnum::Value]));
				$connar1['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar1['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				$count=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['attend_count_school']=$count['attend_count'];

				// 以外聘督导身份听课人数
				$connarnum['items']='count(DISTINCT teacher_name) attend_num';
				$connarnum['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Outside[XPHEnum::Value]));
				$connarnum['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connarnum['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connarnum['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connarnum['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				
				$num=$this->CI->Attend_model->get_single_full($connarnum);
				$data['ret'][$k]['attend_num_outside']=$num['attend_num'];

				// 以外聘督导身份听课次数
				$connar1['items']='COUNT(*) attend_count';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Outside[XPHEnum::Value]));
				$connar1['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar1['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				$count=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['attend_count_outside']=$count['attend_count'];

				// 以教师身份听课人数
				$connarnum['items']='count(DISTINCT teacher_name) attend_num';
				$connarnum['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Teacher[XPHEnum::Value]));
				$connarnum['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connarnum['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connarnum['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connarnum['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				
				$num=$this->CI->Attend_model->get_single_full($connarnum);
				$data['ret'][$k]['attend_num_teacher']=$num['attend_num'];

				// 以教师身份听课次数
				$connar1['items']='COUNT(*) attend_count';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=??',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Teacher[XPHEnum::Value]));
				$connar1['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar1['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				$count=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['attend_count_teacher']=$count['attend_count'];

				//总平均分
				$connar1['items']='avg(score) avg';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? ',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value]));
				$connar['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				// if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
				// 	$connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::Unit[XPHEnum::Value]));
				// }else(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				// 	 $connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::School[XPHEnum::Value]));
				// }
				$avg_level=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_total_score']=sprintf("%.2f",$avg_level['avg']);

					//校督导听课平均分
				$connar1['items']='avg(score) avg';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=?? ',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::School[XPHEnum::Value]));
				$connar['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				// if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
				// 	$connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::Unit[XPHEnum::Value]));
				// }else(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				// 	 $connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::School[XPHEnum::Value]));
				// }
				$avg_level=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_school_score']=sprintf("%.2f",$avg_level['avg']);


			//e二级督导听课平均分
				$connar1['items']='avg(score) avg';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=?? ',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Unit[XPHEnum::Value]));
				$connar['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				// if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
				// 	$connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::Unit[XPHEnum::Value]));
				// }else(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				// 	 $connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::School[XPHEnum::Value]));
				// }
				$avg_level=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_unit_score']=sprintf("%.2f",$avg_level['avg']);


						//外聘督导听课平均分
				$connar1['items']='avg(score) avg';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=?? ',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Outside[XPHEnum::Value]));
				$connar['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				// if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
				// 	$connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::Unit[XPHEnum::Value]));
				// }else(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
				// 	 $connar1['where'].=$this->CI->sqlEscape(' and type=??',array(AttendType::School[XPHEnum::Value]));
				// }
				$avg_level=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_outside_score']=sprintf("%.2f",$avg_level['avg']);

				//外聘督导听课平均分
				$connar1['items']='avg(score) avg';
				$connar1['where']=$this->CI->sqlEscape('listen_id=?? and db_attend.status=?? and type=?? ',array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],AttendType::Outside[XPHEnum::Value]));
				$connar['join']=array('db_attend_record','db_attend_record.attend_id=db_attend.id');
				if(isset($params['content'])){
					$connar['where'].=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'].=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$connar1['where'].=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				$avg_level=$this->CI->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_teacher_score']=sprintf("%.2f",$avg_level['avg']);

				// 总分最高分
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$where=$this->CI->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($param['end_time']." 23:59:59")));
				}else if (isset($param['start_time']) && isset($param['end_time'])) {
					$where=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}else{
					$where='';
				}
				if(isset($param['content'])){
					$where.=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($param['dept_name'])) {
					$where.=' and db_attend.dept_name like "%'.$param['dept_name'].'%"';
				}
				if (isset($param['username'])) {
					$where.=' and db_admin.username like "%'.$param['username'].'%"';
				}
				if (isset($param['level']) && $param['level']!=-1) {
					if($param['level']==FeedBackSubType::School[XPHEnum::Value]){
						$level=WebRole::SchoolSupervisor[XPHEnum::Value];
					}
					if($param['level']==FeedBackSubType::Unit[XPHEnum::Value]){
						$level=WebRole::UnitSupervisor[XPHEnum::Value];
					}
					$where.=' and (role_id='.$level.' or find_in_set('.$level.',sub_role_id))';
				}

				
				$sql=$this->CI->sqlEscape("select * from db_attend  left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id where score=(SELECT max(score) from db_attend  left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id  where listen_id=??  and db_attend.status=?? ".$where." ) and listen_id=??  and db_attend.status=?? ".$where." group by teacher_name",array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],$v['listen_id'],AttendStatus::Submit[XPHEnum::Value]));
				$max=$this->CI->Attend_model->query($sql);
				$max_score=$max[0]['score'];
				$max_teacher_name=implode(',', array_column($max,'teacher_name'));
				$data['ret'][$k]['max_score']=sprintf("%.2f",$max_score).'('.$max_teacher_name.')';


				// 总分最低分
				$sql=$this->CI->sqlEscape("select * from db_attend left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id  where score=(SELECT min(score)  from db_attend  left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id where listen_id=??  and db_attend.status=?? ".$where." ) and listen_id=??  and db_attend.status=?? ".$where." group by teacher_name",array($v['listen_id'],AttendStatus::Submit[XPHEnum::Value],$v['listen_id'],AttendStatus::Submit[XPHEnum::Value]));
				$min=$this->CI->Attend_model->query($sql);
				$min_score=$min[0]['score'];
				$min_teacher_name=implode(',', array_column($min,'teacher_name'));
				$data['ret'][$k]['min_score']=sprintf("%.2f",$min_score).'('.$min_teacher_name.')';

				$data['ret'][$k]['num']=$k+1;

			}
			
			if (!$isExport) {

				$attend_num_unit=0;
				$attend_count_unit=0;
				$attend_num_school=0;
				$attend_count_school=0;
				$attend_num_outside=0;
				$attend_count_outside=0;
				$avg_total_score=0;
				$avg_school_score=0;
				$avg_unit_score=0;
				$avg_outside_score=0;

				foreach ($data['ret'] as $k => $v) {
					$attend_num_unit+=$v['attend_num'];
					$attend_count_unit+=$v['attend_count'];
					$attend_num_school+=$v['attend_num_school'];
					$attend_count_school+=$v['attend_count_school'];

					$attend_num_outside+=$v['attend_num_outside'];
					$attend_count_outside+=$v['attend_count_outside'];

					$avg_total_score+=$v['avg_total_score'];
					$avg_school_score+=$v['avg_school_score'];
					$avg_unit_score+=$v['avg_unit_score'];
					$avg_outside_score+=$v['avg_outside_score'];

				}

				$count = count($data['ret']);

				$data['ret'][$count]['username']= '合计';
				$data['ret'][$count]['attend_num']=$attend_num_unit;
				$data['ret'][$count]['attend_count']=$attend_count_unit;
				$data['ret'][$count]['attend_num_school']=$attend_num_school;
				$data['ret'][$count]['attend_count_school']=$attend_count_school;
				$data['ret'][$count]['attend_num_outside']=$attend_num_outside;
				$data['ret'][$count]['attend_count_outside']=$attend_count_outside;
				$data['ret'][$count]['avg_total_score']=$avg_total_score;
				$data['ret'][$count]['avg_school_score']=$avg_school_score;
				$data['ret'][$count]['avg_unit_score']=$avg_unit_score;
				$data['ret'][$count]['avg_outside_score']=$avg_outside_score;
				$data['ret'][$count]['max_score']='';
				$data['ret'][$count]['min_score']='';

				if(count($data['ret'])!=0){

				// 总分最高分			
				$where=" and 1=1";
				if (isset($param['start_time']) && isset($param['end_time'])) {
					$where.=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				if(isset($param['content'])){
					$where.=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($param['dept_name'])) {
					$where.=' and db_attend.dept_name like "%'.$param['dept_name'].'%"';
				}
				if (isset($param['username'])) {
					$where.=' and db_admin.username like "%'.$param['username'].'%"';
				}
				if (isset($param['level']) && $param['level']!=-1) {
					if($param['level']==FeedBackSubType::School[XPHEnum::Value]){
						$level=WebRole::SchoolSupervisor[XPHEnum::Value];
					}
					if($param['level']==FeedBackSubType::Unit[XPHEnum::Value]){
						$level=WebRole::UnitSupervisor[XPHEnum::Value];
					}
					$where.=' and (role_id='.$level.' or find_in_set('.$level.',sub_role_id))';
				}
				$sql=$this->CI->sqlEscape("select * from db_attend left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id where score=(SELECT max(score) from db_attend  left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id  where db_attend.status=?? ".$where." ) and db_attend.status=?? ".$where." group by teacher_name",array(AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
				$max=$this->CI->Attend_model->query($sql);	
				if($max){
					$max_score=$max[0]['score'];
					$max_teacher_name=implode(',', array_column($max,'teacher_name'));
					$max_level=sprintf("%.2f",$max_score).'('.$max_teacher_name.')';
				}else{
					$max_level = '';
				}		
					
				$sql=$this->CI->sqlEscape("select * from db_attend left join db_attend_record on db_attend_record.attend_id=db_attend.id   left join db_admin on db_admin.id=db_attend.listen_id where score=(SELECT min(score) from db_attend  left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id  where db_attend.status=?? ".$where." ) and db_attend.status=?? ".$where." group by teacher_name",array(AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
				$min=$this->CI->Attend_model->query($sql);		

				if($min){
					$min_score=$min[0]['score'];
					$min_teacher_name=implode(',', array_column($min,'teacher_name'));
					$min_level=sprintf("%.2f",$min_score).'('.$min_teacher_name.')';
				}else{
					$min_level= '';
				}			
				
				$data['ret'][$count]['max_score']=$max_level;
				$data['ret'][$count]['min_score']=$min_level;
			}
				//echo $count; exit;

				

				echo json_encode($data);
			} else {
				// return $data["ret"];

				// 合计
				$attend_num_unit=0;
				$attend_count_unit=0;
				$attend_num_school=0;
				$attend_count_school=0;
				$attend_num_outside=0;
				$attend_count_outside=0;
				$avg_total_score=0;
				$avg_school_score=0;
				$avg_unit_score=0;
				$avg_outside_score=0;

				foreach ($data['ret'] as $k => $v) {
					$attend_num_unit+=$v['attend_num'];
					$attend_count_unit+=$v['attend_count'];
					$attend_num_school+=$v['attend_num_school'];
					$attend_count_school+=$v['attend_count_school'];

					$attend_num_outside+=$v['attend_num_outside'];
					$attend_count_outside+=$v['attend_count_outside'];

					$avg_total_score+=$v['avg_total_score'];
					$avg_school_score+=$v['avg_school_score'];
					$avg_unit_score+=$v['avg_unit_score'];
					$avg_outside_score+=$v['avg_outside_score'];

				}	


				if(count($data['ret'])!=0){

				// 总分最高分			
				$where=" and 1=1";
				if (isset($param['start_time']) && isset($param['end_time'])) {
					$where.=' and db_attend.listen_time>'.strtotime($param['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($param['end_time']." 23:59:59");
				}
				if(isset($param['content'])){
					$where.=' and db_attend_record.commentary like "%'.$param['content'].'%"';
				}
				if (isset($param['dept_name'])) {
					$where.=' and db_attend.dept_name like "%'.$param['dept_name'].'%"';
				}
				if (isset($param['username'])) {
					$where.=' and db_admin.username like "%'.$param['username'].'%"';
				}
				if (isset($param['level']) && $param['level']!=-1) {
					if($param['level']==FeedBackSubType::School[XPHEnum::Value]){
						$level=WebRole::SchoolSupervisor[XPHEnum::Value];
					}
					if($param['level']==FeedBackSubType::Unit[XPHEnum::Value]){
						$level=WebRole::UnitSupervisor[XPHEnum::Value];
					}
					$where.=' and (role_id='.$level.' or find_in_set('.$level.',sub_role_id))';
				}
				$sql=$this->CI->sqlEscape("select * from db_attend left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id where score=(SELECT max(score) from db_attend  left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id  where db_attend.status=?? ".$where." ) and db_attend.status=?? ".$where." group by teacher_name",array(AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
				$max=$this->CI->Attend_model->query($sql);			
				$max_score=$max[0]['score'];
				$max_teacher_name=implode(',', array_column($max,'teacher_name'));
				$max_level=sprintf("%.2f",$max_score).'('.$max_teacher_name.')';

			
				$sql=$this->CI->sqlEscape("select * from db_attend left join db_attend_record on db_attend_record.attend_id=db_attend.id   left join db_admin on db_admin.id=db_attend.listen_id where score=(SELECT min(score) from db_attend  left join db_attend_record on db_attend_record.attend_id=db_attend.id left join db_admin on db_admin.id=db_attend.listen_id  where db_attend.status=?? ".$where." ) and db_attend.status=?? ".$where." group by teacher_name",array(AttendStatus::Submit[XPHEnum::Value],AttendStatus::Submit[XPHEnum::Value]));
				$min=$this->CI->Attend_model->query($sql);			
				$min_score=$min[0]['score'];
				$min_teacher_name=implode(',', array_column($min,'teacher_name'));
				$min_level=sprintf("%.2f",$min_score).'('.$min_teacher_name.')';

				array_push($data['ret'],array('num'=>'合计','usercode'=>'','username'=>'','attend_num'=>$attend_num_unit,'attend_count'=>$attend_count_unit,'attend_num_school'=>$attend_num_school,'attend_count_school'=>$attend_count_school,'attend_num_outside'=>$attend_num_outside,'attend_count_outside'=>$attend_count_outside,'avg_total_score'=>$avg_total_score,'avg_school_score'=>$avg_school_score,'avg_unit_score'=>$avg_unit_score,'avg_outside_score'=>$avg_outside_score,'max_score'=>$max_level,'min_score'=>$min_level));

			}else{
				array_push($data['ret'],array('num'=>'合计','usercode'=>'','username'=>'','attend_num'=>'','attend_count'=>'','attend_num_school'=>'','attend_count_school'=>'','attend_num_outside'=>'','attend_count_outside'=>'','avg_total_score'=>'','avg_school_score'=>'','avg_unit_score'=>'','avg_outside_score'=>'','max_score'=>'','min_score'=>''));
			}

			$this-> load ->library('PHPExcel/PHPExcel');
			$excel = new PHPExcel();
				//公共样式
			$excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11); 
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
			$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

			$x_line=0;
			$y_line=1;

				//标题
			// $title=array($param['start_time'].'至'.$param['end_time'],'工号','姓名','督导听课人数','督导听课次数','校督导听课人数','校督导听课次数','教学水平平均分','教学效果平均分','教学水平90分(含)以上','教学水平80以下','教学效果90分(含)以上','教学效果80以下','教学水平最高分','教学水平最低分','教学效果最高分','教学效果最低分');
			$title=array($param['start_time'].'至'.$param['end_time'],'工号','姓名','督导听课人数','督导听课次数','校督导听课人数','校督导听课次数','外聘督导听课人数','外聘督导听课次数','总分平均分','校督导平均分','二级督导平均分','外聘督导平均分','总分最高分','总分最低分');

			foreach ($title as $k => $v) {
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line,$v);
				$excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line)->getFont()->setBold(true);   
				$x_line++;
			}

			//pri($data['ret']);exit;

			$x_line=0;
			$y_line=2;
				//正式数据
			$count=count($data['ret']);
			$num=0;
			foreach ($data['ret'] as $k => $v) {
				$num++;	 
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0).$y_line,$v['num']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(1).$y_line,$v['usercode']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(2).$y_line,$v['username']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(3).$y_line,$v['attend_num']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(4).$y_line,$v['attend_count']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(5).$y_line,$v['attend_num_school']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(6).$y_line,$v['attend_count_school']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(7).$y_line,$v['attend_num_outside']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(8).$y_line,$v['attend_count_outside']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(9).$y_line,$v['avg_total_score']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(10).$y_line,$v['avg_school_score']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(11).$y_line,$v['avg_unit_score']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(12).$y_line,$v['avg_outside_score']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(13).$y_line,$v['max_score']);
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(14).$y_line,$v['min_score']);
				$y_line++;
			}

			$write = new PHPExcel_Writer_Excel2007($excel); 
			$director = "upload/export/" . date('Ymd', time()) . "/";
			if (!file_exists($director)) {
					//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($director, 0777);
			}        
			$file_name ='个人听课统计报表'.date('Ymd').'.xlsx';       	
			$write->save($director.iconv('utf-8', 'gb2312', $file_name));
			exit(retJson('返回地址',true,$director.$file_name));
		}
	}
	

	

	
	
}